package com.example.springboot3.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class User1Aspect {
    @Pointcut("execution(* com.example.springboot3.controller.User1Controller.*(..))")
    public void pointCut(){

    }
    @Before("pointCut()")
    public void before(){
        System.out.println();
        System.out.println("执行前置通知");
        System.out.println();
    }
    @After("pointCut()")
    public void after(){
        System.out.println();
        System.out.println("执行后置通知");
        System.out.println();
    }
    @AfterReturning("pointCut()")
    public void afterReturning(){
        System.out.println();
        System.out.println("return之前通知");
        System.out.println();
    }
    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        System.out.println();
        System.out.println("抛出异常之后通知");
        System.out.println();
    }
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object object = null;
        System.out.println("环绕前置通知");
        try {
            //执行拦截方法
            object = joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        System.out.println("环绕后置通知");
        return object;
    }
}
